//
//  ViewController.m
//  17作业
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "FirstViewController.h"
#import "CustomTableViewCell.h"
#import "Model.h"
#import "DataBaseManager.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,FirstViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *arr;
@property (nonatomic,strong) NSMutableArray *nameArr;
@property (nonatomic,strong) NSMutableArray *modelArr;
@property (nonatomic,strong) DataBaseManager *dataManager;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataManager = [DataBaseManager shareManager];
    
    
    
   _arr = [NSArray arrayWithContentsOfFile:@"/Users/dllo/DLS160710/UI/HomeWork/17作业/17作业/DLS160710(1).plist"];
    

    for (NSDictionary *dic in _arr) {
        NSString *nameStr = [dic objectForKey:@"name"];
        NSString *genderStr = [dic objectForKey:@"gender"];
        NSString *phoneNumberStr = [dic objectForKey:@"phoneNumber"];
        NSNumber *ageStr1 = [dic objectForKey:@"age"];
        NSString *ageStr = [ageStr1 stringValue];
        Model *model = [[Model alloc] initWithName:nameStr gender:genderStr phoneNumber:phoneNumberStr age:ageStr];
        [_modelArr addObject:model];
}
    
    NSLog(@"%@",_modelArr);
   
    
    

    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.rowHeight = 80;
    
    UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addContact:)];
    self.navigationItem.rightBarButtonItem = addBtn;
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

- (void)createData
{
    _modelArr = [NSMutableArray array];
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSFileManager *fm = [NSFileManager defaultManager];
    [fm changeCurrentDirectoryPath:@"model.db"];
    [_dataManager openDataBase];
    
    _modelArr = [_dataManager selectAll];
    if (_modelArr.count == 0) {
        for (NSDictionary *dic in _arr) {
            NSString *nameStr = [dic objectForKey:@"name"];
            NSString *genderStr = [dic objectForKey:@"gender"];
            NSString *phoneNumberStr = [dic objectForKey:@"phoneNumber"];
            NSNumber *ageStr1 = [dic objectForKey:@"age"];
            NSString *ageStr = [ageStr1 stringValue];
            Model *model = [[Model alloc] initWithName:nameStr gender:genderStr phoneNumber:phoneNumberStr age:ageStr];
            [_modelArr addObject:model];
            
        }
    }
    
}

- (void)saveData
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    [_dataManager openDataBase];
    [_dataManager createTable];
    for (Model *model in _modelArr) {
        UIImage *image = [UIImage imageNamed:@"0"];
        NSData *imageData = UIImageJPEGRepresentation(image, 0);
        NSString *imagePath = [path stringByAppendingPathComponent:@"contect.db"];
        [imageData writeToFile:imagePath atomically:YES];
        [_dataManager insertModel:model];
    }
}

- (void)addContact:(UIBarButtonItem *)btn
{
    FirstViewController *fvc = [[FirstViewController alloc] init];
    fvc.delegate = self;
    [self.navigationController pushViewController:fvc animated:YES];
//    Model *model = [_modelArr firstObject];
//    [_modelArr insertObject:model atIndex:0];
//    [_tableView reloadData];
}

- (void)sendModel:(Model *)model
{
    [_modelArr addObject:model];
    [_tableView reloadData];
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    UIImage *image = [UIImage imageNamed:@"0"];
    NSData *imageData = UIImageJPEGRepresentation(image, 0);
    NSString *imagePath = [path stringByAppendingPathComponent:@"model.db"];
    [imageData writeToFile:imagePath atomically:YES];
    [_dataManager insertModel:model];
    
}

//1
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    
}
//2
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//3
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _nameArr.count;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        NSLog(@"插入");
        [_nameArr insertObject:@"小子" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    } else if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
        [_nameArr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    cell.textLabel.text = _nameArr[indexPath.row];
    cell.model = _modelArr[indexPath.row];
    return cell;

}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    [_nameArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    
}

//- (void)reloadData
//{
//    [_tableView reloadData];
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FirstViewController *fvc = [[FirstViewController alloc] init];
    fvc.model = _modelArr[indexPath.row];
    [self.navigationController pushViewController:fvc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
